package com.xrs.xrseducation.teacherdetails.controller;

import com.xrs.xrseducation.teacherdetails.entity.Teacherdetails;
import com.xrs.xrseducation.teacherdetails.service.impl.TeacherdetailsServiceImpl;
import com.xrs.xrseducation.util.JsonUtils;
import com.xrs.xrseducation.util.LayuiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 教师详情表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-09-18
 */
@RestController
@RequestMapping("/teacherdetails/teacherdetails")
public class TeacherdetailsController {

    @Autowired
    private TeacherdetailsServiceImpl teacherdetailsService;
    /**
     * 查询所有教师信息
     */
    @RequestMapping("/queryAllTeacher")
    @ResponseBody
    public LayuiUtils queryAllTeacher(){
        LayuiUtils layuiUtils=new LayuiUtils();
        List<Teacherdetails> teacherdetails=teacherdetailsService.list();
        layuiUtils.setCode(0);
        layuiUtils.setCount(teacherdetails.size());
        layuiUtils.setMsg("查询成功！");
        layuiUtils.setData(teacherdetails);
        return layuiUtils;
    }

    public JsonUtils fileupdaload(@RequestParam("file") MultipartFile picture, HttpServletResponse response, HttpServletRequest request) throws IOException {
        //设置图片名称
        String pictureName = UUID.randomUUID().toString();
//获取文件名
        String fileName = picture.getOriginalFilename();
//图片的后缀
        String suffix = fileName.substring(fileName.lastIndexOf("."));
//uuid 图片名称
        String path=pictureName+suffix;
//获取当前项目的路径
        File fileP=new File(ResourceUtils.getURL("classpath:").getPath());
//获取指定文件夹的路径（项目中存放静态文件的地址）
        File imagePath=new File(fileP.getAbsolutePath(),"static/img/fileimge");
//上传的文件
        File imgSrc=new File(imagePath.toString(),path);
//上传
        picture.transferTo(imgSrc);
        JsonUtils jsonUtils=new JsonUtils();
        jsonUtils.setCode(0);
        jsonUtils.setMsg("");
//添加进数据库的图片地址
        jsonUtils.setData(path);
        return jsonUtils;
    }

    /**
     * 添加一条教师信息
     * @param teacherdetails
     * @return
     */
    @RequestMapping("/addTeacher")
    public String addTeacher(Teacherdetails teacherdetails){
        teacherdetailsService.save(teacherdetails);
        return "";
    }

    /**
     * 根据id查询一条教师信息
     * @param tid
     * @return
     */
    @RequestMapping("/selectByIdTeacher")
    public String selectByIdTeacher(Integer tid){
        teacherdetailsService.getById(tid);
        return "";
    }

    /**
     * 修改一条教师信息
     * @param teacherdetails
     * @return
     */
    @RequestMapping("/updateTeacherById")
    public String updateTeacherById(Teacherdetails teacherdetails){
        teacherdetailsService.updateById(teacherdetails);
        return "";
    }

    @RequestMapping("/deleteTeacher")
    public String deleteTeacher(Integer tid){
        teacherdetailsService.removeById(tid);
        return "";
    }
}
